package com.smithlibraties.util.annotation.register.render;

import com.smithlibraties.SmithLibraries;
import com.smithlibraties.util.annotation.AnnotationClassGetter;
import com.smithlibraties.util.annotation.register.RegisterEntity;
import cpw.mods.fml.client.registry.RenderingRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.entity.Entity;

import java.util.ArrayList;
import java.util.List;

@SideOnly(Side.CLIENT)
public class RegisterEntityRender {
    List<EntityRenderAttribute> list = new ArrayList<>();

    @SideOnly(Side.CLIENT)
    public RegisterEntityRender() {
        List<Class> entityRenderCollections = AnnotationClassGetter.getAllClassByInterface(SmithLibraries.getAllClasses(), RegisterEntity.EntityRender.class);//获取实现RegisterClass注释的类的List
        for (Class clazz : entityRenderCollections) {//开始集中继承IRegister接口的的类的属性的List
            list = RegisterEntityRender.getAnnotationList(clazz);
        }
        for (EntityRenderAttribute registerAttribute : list) {
            try {
                registerEntityRender(registerAttribute.getClazz1(), registerAttribute.getClazz().newInstance());
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    @SideOnly(Side.CLIENT)
    private static void registerEntityRender(Class<? extends Entity> clazz, Object object) {
        RenderingRegistry.registerEntityRenderingHandler(clazz, (Render) object);
    }

    @SideOnly(Side.CLIENT)
    private static List<EntityRenderAttribute> getAnnotationList(Class clazz) {
        List<EntityRenderAttribute> list1 = new ArrayList<>();
        if (clazz.isAnnotationPresent(RegisterEntity.EntityRender.class)) {
            RegisterEntity.EntityRender registerEntityRender = (RegisterEntity.EntityRender) clazz.getAnnotation(RegisterEntity.EntityRender.class);
            list1.add(new EntityRenderAttribute(clazz, registerEntityRender.clazz()));
        }
        return list1;
    }
}
